<template>
  <div class="dashboard">
    <div class="welcome-section">
      <h1>欢迎进入流浪动物救助平台后台管理</h1>
      <p>让我们一起为流浪动物创造更好的未来</p>
    </div>

    <div class="stats-container">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-card class="stat-card" shadow="hover">
            <template #header>
              <div class="card-header">
                <el-icon><User /></el-icon>
                <span>用户总数</span>
              </div>
            </template>
            <div class="card-content">
              <h2>{{ userCount }}</h2>
              <p>活跃用户</p>
            </div>
          </el-card>
        </el-col>

        <el-col :span="6">
          <el-card class="stat-card" shadow="hover">
            <template #header>
              <div class="card-header">
                <el-icon><Paw /></el-icon>
                <span>救助动物</span>
              </div>
            </template>
            <div class="card-content">
              <h2>{{ petCount }}</h2>
              <p>已救助动物</p>
            </div>
          </el-card>
        </el-col>

        <el-col :span="6">
          <el-card class="stat-card" shadow="hover">
            <template #header>
              <div class="card-header">
                <el-icon><House /></el-icon>
                <span>待领养</span>
              </div>
            </template>
            <div class="card-content">
              <h2>{{ availablePetCount }}</h2>
              <p>等待领养</p>
            </div>
          </el-card>
        </el-col>

        <el-col :span="6">
          <el-card class="stat-card" shadow="hover">
            <template #header>
              <div class="card-header">
                <el-icon><Calendar /></el-icon>
                <span>爱心捐赠</span>
              </div>
            </template>
            <div class="card-content">
              <h2>{{ totalAmount }}</h2>
              <p>捐赠总金额</p>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <div class="charts-container">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-card class="chart-card" shadow="hover">
            <template #header>
              <div class="card-header">
                <span>救助动物类型分布</span>
              </div>
            </template>
            <v-chart class="chart" :option="pieOption" autoresize />
          </el-card>
        </el-col>
        <el-col :span="12">
          <el-card class="chart-card" shadow="hover">
            <template #header>
              <div class="card-header">
                <span>近期救助趋势</span>
              </div>
            </template>
            <v-chart class="chart" :option="lineOption" autoresize />
          </el-card>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
import { PieChart, LineChart } from 'echarts/charts'
import { GridComponent, TooltipComponent, LegendComponent, TitleComponent } from 'echarts/components'
import VChart from 'vue-echarts'
import { User, House, Calendar } from '@element-plus/icons-vue'
import request from '@/utils/request'

use([
  CanvasRenderer,
  PieChart,
  LineChart,
  GridComponent,
  TooltipComponent,
  LegendComponent,
  TitleComponent
])

const userCount = ref(0)
const petCount = ref(0)
const availablePetCount = ref(0)
const totalAmount = ref(0)
const petTypes = ref([])

onMounted(async () => {
  try {
    const [usersRes, petsRes, availablePetsRes, donationStats, typesRes] = await Promise.all([
      request.get('/users/admin'),
      request.get('/pet/getAllPets'),
      request.get('/pet/petSearch', {
        params: {
          status: 'available',
          pageNum: 1,
          pageSize: 999999
        }
      }),
      request.get('/donation/statistics'),
      request.get('/types/all')
    ])
    userCount.value = usersRes.data.length
    petCount.value = petsRes.data.length
    availablePetCount.value = availablePetsRes.data.total || 0
    totalAmount.value = donationStats.data.totalAmount || 0
    
    // 处理动物类型数据
    petTypes.value = typesRes.data.map(type => ({
      value: 0, // 初始值设为0
      name: type.type,
      id: type.id
    }))
    
    // 统计每种类型的数量
    petsRes.data.forEach(pet => {
      const typeIndex = petTypes.value.findIndex(t => t.id === pet.pet_type_id)
      if (typeIndex !== -1) {
        petTypes.value[typeIndex].value++
      }
    })
    
    // 更新饼图数据
    pieOption.value.series[0].data = petTypes.value
  } catch (error) {
    console.error('获取数据失败:', error)
  }
})

const pieOption = ref({
  tooltip: {
    trigger: 'item'
  },
  legend: {
    orient: 'vertical',
    left: 'left'
  },
  series: [
    {
      name: '动物类型',
      type: 'pie',
      radius: '50%',
      data: [],
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      }
    }
  ]
})

const lineOption = ref({
  tooltip: {
    trigger: 'axis'
  },
  xAxis: {
    type: 'category',
    data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
  },
  yAxis: {
    type: 'value'
  },
  series: [
    {
      name: '救助数量',
      type: 'line',
      data: [12, 15, 8, 23, 17, 35, 19],
      smooth: true
    }
  ]
})
</script>

<style scoped lang="scss">
.dashboard {
  padding: 24px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 60px);
}

.welcome-section {
  text-align: center;
  margin-bottom: 48px;
  padding: 32px 0;
  background: linear-gradient(135deg, #409EFF 0%, #36D1DC 100%);
  border-radius: 12px;
  color: white;

  h1 {
    font-size: 32px;
    font-weight: 600;
    margin-bottom: 16px;
    text-shadow: 0 2px 4px rgba(0,0,0,0.1);
  }

  p {
    font-size: 18px;
    opacity: 0.9;
  }
}

.stats-container {
  margin-bottom: 40px;
}

.stat-card {
  transition: all 0.3s ease;
  border-radius: 12px;
  overflow: hidden;

  &:hover {
    transform: translateY(-5px);
  }

  .card-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 16px;
    background-color: #f8f9fa;
    border-bottom: 1px solid #ebeef5;
    
    .el-icon {
      font-size: 24px;
      color: #409EFF;
    }

    span {
      font-size: 16px;
      font-weight: 600;
      color: #303133;
    }
  }

  .card-content {
    text-align: center;
    padding: 24px 16px;
    
    h2 {
      font-size: 36px;
      font-weight: bold;
      background: linear-gradient(45deg, #409EFF, #36D1DC);
      -webkit-background-clip: text;
      color: transparent;
      margin-bottom: 12px;
    }

    p {
      color: #606266;
      font-size: 15px;
    }
  }
}

.chart-card {
  border-radius: 12px;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-5px);
  }

  .card-header {
    padding: 16px;
    border-bottom: 1px solid #ebeef5;
    
    span {
      font-size: 16px;
      font-weight: 600;
      color: #303133;
    }
  }

  .chart {
    height: 350px;
    padding: 16px;
  }
}
</style>